var moneyList = [{
    ID: 1,
    Name: '线下支付'
}, {
    ID: 2,
    Name: '线上支付'
}];
var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        chargeMoneyTypeList: this.moneyList,
        form: {
            startTime: '',
            endTime: '',
            activeDate: 0,
            ChargeMoneyTypes: [1,2], //筛选  1：线下支付 2 线上支付
            ChargeIDList: [],
            ProjectIDs: [],
            activeDateDesc: '本月',
        },
        data: [],
    },
    methods: {
        get_data: function() {
            var that = this;
            that.doClickDay(2); //点击选择时间
            that.load_data();
        },
        load_data: function() {
            var that = this;
            //统计列表
            var options = {};
            options.action = 'getmallordertjbybuseniss';
            options.PaymentType = that.form.ChargeMoneyTypes;
            options.StartTime = that.form.startTime;
            options.EndTime = that.form.endTime;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    //alert(JSON.stringify(data.list));
                    //alert(JSON.stringify(that.data.ChartData));
                    that.data = data;
                    that.loadChart(that.data.ChartData, that.data.CharTime); //加载统计图
                }
            });
        },
        doClickDay: function(dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate = dateRangeUtil.getDateString(currentDate);
                endDate = startDate;
                that.form.activeDateDesc = '今日';
            }
            if (dataValue == 2) {
                var currentMonth = dateRangeUtil.getCurrentMonth();
                startDate = dateRangeUtil.getDateString(currentMonth[0]);
                endDate = dateRangeUtil.getDateString(currentMonth[1]);
                that.form.activeDateDesc = '本月';
            }
            if (dataValue == 3) {
                var currentYear = dateRangeUtil.getCurrentYear();
                startDate = dateRangeUtil.getDateString(currentYear[0]);
                endDate = dateRangeUtil.getDateString(currentYear[1]);
                that.form.activeDateDesc = '本年';
            }
            that.form.startTime = startDate;
            that.form.endTime = endDate;
            //alert(that.form.startTime);
            //alert(that.form.endTime);
        },
        doSelectSearchParam: function(type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
        },
        doReset: function(type) {
            var that = this;
            if (type == 2) {
                that.form.startTime = '';
                that.form.endTime = '';
                that.form.activeDate = 0;
            } else {
                that.form.ChargeIDList = [];
                that.form.ChargeMoneyTypes = [];
                that.form.ProjectIDs = [];
            }
        },
        doSearch: function() {
            var that = this;
            //alert(that.form.startTime);
            //alert(that.form.endTime);
            //alert(datedifference(that.form.endTime,that.form.startTime));
            if (datedifference(that.form.endTime, that.form.startTime) > 31) {
                api.toast({
                    msg: "查询时间跨度不能超过1个月，请重新输入时间",
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            that.doHideSearch();
            that.load_data();
        },
        doHideSearch: function() {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        getSearchParams: function() {
            var that = this;
            var options = {};
            options.action = "getanalysissearchparam";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.chargeMoneyTypeList = data.chargeMoneyTypeList;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        doSelectChargeMoneyType: function(ID) {
            var that = this;
            var index = that.form.ChargeMoneyTypes.indexOf(ID);
            if (index > -1) {
                that.form.ChargeMoneyTypes.splice(index, 1);
            } else {
                that.form.ChargeMoneyTypes.push(ID);
            }

        },
        do_select_time: function(type) {
            var that = this;
            var currentTime = type == 1 ? that.form.startTime : that.form.endTime;
            api.openPicker({
                type: 'date',
                date: currentTime,
                title: '选择日期'
            }, function(ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentTime = year + '-' + month + '-' + day;
                    if (type == 1) {
                        that.form.startTime = currentTime;
                    } else {
                        that.form.endTime = currentTime;
                    }
                }
            });
        },
        loadChart: function(datas, times) {
            //alert(times);
            var chart = new Highcharts.Chart('container', {
                    credits: {
                        enabled: false
                    },
                    exporting: {
                        enabled: false
                    },
                    title: {
                        text: ''
                    },
                    subtitle: {
                        text: ''
                    },
                    xAxis: {
                        categories: times
                    },
                    yAxis: {
                        title: {
                            text: ''
                        }
                    },
                    legend: {
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'middle'
                    },
                    plotOptions: {
                        line: {
                            dataLabels: {
                                // 开启数据标签
                                enabled: true
                            },
                            // 关闭鼠标跟踪，对应的提示框、点击事件会失效
                            //  enableMouseTracking: false
                        }
                    },
                    series: [{
                        name: '收入',
                        data: datas
                    }],
                    responsive: {
                        rules: [{
                            condition: {
                                maxWidth: 500
                            },
                            chartOptions: {
                                legend: {
                                    layout: 'horizontal',
                                    align: 'center',
                                    verticalAlign: 'bottom'
                                }
                            }
                        }]
                    }
                },
                function(c) { // 图表初始化完毕后的会掉函数
                });
        },
        open_shouru_detail: function(id) {
            var that = this;
            ns.openWin('dl_shouru_detail_frm', '流水明细', {
                id: id,
            });
        },
      //时间戳转换为日期字符串 yyyy-MM-dd HH:mm:ss
        gettime:function(inputTime){
              var date = new Date(inputTime);
              var y = date.getFullYear();
              var m = date.getMonth() + 1;
              m = m < 10 ? ('0' + m) : m;
              var d = date.getDate();
              d = d < 10 ? ('0' + d) : d;
              var h = date.getHours();
              h = h < 10 ? ('0' + h) : h;
              var minute = date.getMinutes();
              var second = date.getSeconds();
              minute = minute < 10 ? ('0' + minute) : minute;
              second = second < 10 ? ('0' + second) : second;
              return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
        },
        getordertype:function(ordertype){
          switch(ordertype)
          {
            case 1:
                return "线下支付";
            break;
              default :
              return "线上支付"
              break;
          }
        }


    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.get_data();
};
//两个时间相差天数 //sDate1和sDate2是2006-12-18格式
function datedifference(sDate1, sDate2) {
    var dateSpan,
        tempDate,
        iDays;
    sDate1 = Date.parse(sDate1);
    sDate2 = Date.parse(sDate2);
    dateSpan = sDate2 - sDate1;
    dateSpan = Math.abs(dateSpan);
    iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
    return iDays
};
